package com.lbs.jiepiaisi.manager;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.bean.EquipmentEqNoEntity;
import com.lbs.jiepiaisi.bean.VinList;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * 时间：2017/6/30 13:34
 * 作用：地图Mark管理类
 */

public class VinListPoiOverlay {
    public static List<PoiItem> poiItems = new ArrayList<>();
    private AMap aMap;
    private ArrayList<Marker> markers = new ArrayList();
    List<VinList.Vin> items=new ArrayList<>();
    List<BitmapDescriptor> bitmapDescriptor;
    private int padding;

    public VinListPoiOverlay(AMap var1, List<VinList.Vin> items, List<BitmapDescriptor> b, int padding) {
        this.aMap = var1;
        this.items = items;
        this.bitmapDescriptor = b;
        this.padding = padding;

        for (int i = 0; i < items.size(); i++) {
            LatLonPoint latLotPoint=null;
            Double lat=null;
            Double lng=null;
            if(items.get(i).getPoint() != null){
                lat=items.get(i).getPoint().getLat();
                lng=items.get(i).getPoint().getLng();
                latLotPoint = new LatLonPoint(lat,lng);
                PoiItem mPoiItem = new PoiItem(null, latLotPoint, null, null);
                this.poiItems.add(mPoiItem);
            }
        }
    }
    public Marker getMarkerByPosition(int position){
        if(position < 0){
            return null;
        }
        return markers.get(position);
    }

    /**
     * 添加Mark到地图中
     */
    public void addToMap() {
        Marker marker=null;
        Log.e("TAG","items.size():"+items.size());


        for (int i = 0; i < items.size(); ++i){
            MarkerOptions markerOptions = getMarker(i);
            if(markerOptions != null){
                marker  = aMap.addMarker(markerOptions);
                marker .setObject(Integer.valueOf(i));
                markers.add(marker);
            }


        }


    }
    /**
     * 刷新标注点到地图中
     */
    public void changeToMap(List<VinList.Vin> items) {
        this.items = items;
        aMap.reloadMap();
    }
    /**
     * 去掉PoiOverlay上所有的Marker。
     */
    public void removeFromMap() {
        Iterator var1 = this.markers.iterator();
        while (var1.hasNext()) {
            Marker var2 = (Marker) var1.next();
            var2.remove();
        }

    }
    /**
     * 移动镜头到当前的视角。
     */
    public void zoomToSpan() {
        try {
            if (this.items != null && this.items.size() > 0) {
                if (this.items == null) {
                    return;
                }
                if (this.items.size() == 1) {
                    this.aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(this.items.get(0).getPoint().getLat(), this.items.get(0).getPoint().getLng()), 13.0F));
                } else {
                    LatLngBounds var1 = this.getAllMarkers();
                    this.aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(var1, padding));
                }
            }
        } catch (Throwable var2) {
            var2.printStackTrace();
        }

    }

    private LatLngBounds getAllMarkers() {
        LatLngBounds.Builder var1 = LatLngBounds.builder();
        for (int var2 = 0; var2 < this.poiItems.size(); ++var2) {
            var1.include(new LatLng(poiItems.get(var2).getLatLonPoint().getLatitude(), poiItems.get(var2).getLatLonPoint().getLongitude()));
        }

        return var1.build();
    }

    private MarkerOptions getMarker(int var1) {

        MarkerOptions marker = new MarkerOptions().anchor(0.5f, 0.5f)
                .title(""+ var1)
                .icon(bitmapDescriptor.get(var1));
        //marker.rotateAngle((-items.get(var1).getPoint().getRoate()));
        if(items.get(var1).getPoint() != null){
            marker.position(new LatLng(items.get(var1).getPoint().getLat(), items.get(var1).getPoint().getLng()));
        }
        //marker.position(new LatLng(items.get(var1).getPoint().getLat(),items.get(var1).getPoint().getLng()));

       // marker.period(var1);

        return marker;
    }



    protected BitmapDescriptor getBitmapDescriptor(int var1) {
        return null;
    }

    protected String getTitle(int var1) {
        return this.poiItems.get(var1).getTitle();
    }

    protected String getSnippet(int var1) {
        return this.poiItems.get(var1).getSnippet();
    }

    /**
     * 从marker中得到poi在list的位置。
     * @Marker 一个标记对象
     * @return   返回该marker对应的poi在list的位置。
     */
    public int getPoiIndex(Marker var1) {
        for (int var2 = 0; var2 < this.markers.size(); ++var2) {
            if (this.markers.get(var2).equals(var1)) {
                return var2;
            }
        }

        return -1;
    }
    /**
     * 返回第index的poi的信息。
     */
    public PoiItem getPoiItem(int var1) {
        return var1 >= 0 && var1 < this.poiItems.size() ? this.poiItems.get(var1) : null;
    }
    /**
     * 为当前Mark点设置图标
     */
    public static List<BitmapDescriptor> getBitmap(List<VinList.Vin> items, Context mContext) {
        List<BitmapDescriptor> mList = new ArrayList<>();
        for (int i = 0; i < items.size(); i++) {
            View view = View.inflate(mContext, R.layout.layout_marker, null);
            ImageView iv = (ImageView) view.findViewById(R.id.iv_marker);
            TextView tv_carname = (TextView) view.findViewById(R.id.tv_carname);
            tv_carname.setText(items.get(i).getOwnerName());
            //判断设备状态
            if(items.get(i).getStatus() != null && items.get(i).getStatus().equals("2")){//离线
                iv.setImageResource(R.mipmap.icon_hui_daohang);
            }else{//不是离线状态
                iv.setImageResource(R.mipmap.icon_daohang);
            }
            if(items.get(i).getPoint() != null){
                iv.setRotation(Float.parseFloat(items.get(i).getPoint().getDir()+""));
            }
            Bitmap bitmap = convertViewToBitmap(view);
            mList.add(BitmapDescriptorFactory.fromBitmap(bitmap));
        }

        return mList;
    }
    /**
     * 将View转换为Bitmap的方法
     */
    //view 转bitmap

    public static Bitmap convertViewToBitmap(View view) {

        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        view.buildDrawingCache();

        Bitmap bitmap = view.getDrawingCache();

        return bitmap;
    }

}